package com.ymatou.iapi.csim.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.csim.parameter.CloseSessionBean;
import com.ymatou.iapi.csim.service.CloseSessionCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CsimCallService;
import com.ymttest.database.model.ymt_cssession;
import com.ymttest.database.model.ymt_onlinecs;
import com.ymttest.database.sqlwapper.CSIM.ymt_cssessionWapper;
import com.ymttest.database.sqlwapper.CSIM.ymt_onlinecsWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
/**
*关闭会话
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CloseSession{
	private static CloseSessionBean closesessionBean;
	private static CloseSessionCall closesessionCall;
	private CsimCallService service=new CsimCallService();
	private Integer csId=Integer.parseInt(EnvSetup.getData("csId").toString());
	private Integer userId=Integer.parseInt(EnvSetup.getData("userId_IM").toString());
	private static ymt_cssessionWapper csSessionWapper=new ymt_cssessionWapper();
	private static ymt_onlinecsWapper onlinecsWapper=new ymt_onlinecsWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("关闭会话");
	}

	@Before
	public void caseUp() {
		closesessionBean=new CloseSessionBean();
		closesessionCall=new CloseSessionCall();
		onlinecsWapper.deleteBy(MapUtil.hashMap("CSId",csId));
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(closesessionCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CloseSession_001() {
		Logger.start(true,"关闭会话-验证正常关闭");
		try {
    		service.getOnlineCS();
    		//打开会话
    		ymt_cssession csSession=service.getOpenSession(csId,userId);
    		ymt_onlinecs onlinecs1=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csId)).get(0);
    		
			closesessionBean.setSessionId(csSession.getId());
			closesessionCall.setData(closesessionBean);
			closesessionCall.callService();	
			
			Logger.verifyEquals("200", closesessionCall.getStatus(), "验证Status");
			Logger.verifyEquals("", closesessionCall.getMsg(), "验证Msg");
			Logger.verifyEquals("true", closesessionCall.getResult().get("Success").toString(), "验证Success");
			
			ymt_cssession mdoel=csSessionWapper.selectByPrimaryKey(csSession.getId());
			Logger.verifyEquals(1, mdoel.getStatus(), "验证Status=1");
			ymt_onlinecs onlinecs2=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csId)).get(0);
			Logger.verifyEquals(1, onlinecs1.getCurrentSessions()-onlinecs2.getCurrentSessions(), "验证最大会话数 减 1");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CloseSession_002() {
		Logger.start(true,"关闭会话-验证会话已关闭");
		try {
    		service.getOnlineCS();
    		//打开会话
    		ymt_cssession csSession=service.getOpenSession(csId,userId);
    		csSession.setStatus(1);
    		csSessionWapper.updateByPrimaryKey(csSession);
			closesessionBean.setSessionId(csSession.getId());
			closesessionCall.setData(closesessionBean);
			closesessionCall.callService();	
			
			Logger.verifyEquals("500", closesessionCall.getStatus(), "验证Status");
			Logger.verifyEquals(true, closesessionCall.getMsg().contains("会话不存在或会话已关闭"), "验证Msg");
			Logger.verifyEquals("false", closesessionCall.getResult().get("Success").toString(), "验证Success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CloseSession_003() {
		Logger.start(true,"关闭会话-验证会话来源微信");
		try {
    		service.getOnlineCS();
    		//打开会话
    		ymt_cssession csSession=service.getOpenSession(csId,userId);
    		csSession.setMessageSource(1);//消息来源，来源于app， 微信，前端传过来
    		csSession.setSourceParams("微信");
    		csSessionWapper.updateByPrimaryKey(csSession);
			closesessionBean.setSessionId(csSession.getId());
    		ymt_onlinecs onlinecs1=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csId)).get(0);
    		
			closesessionCall.setData(closesessionBean);
			closesessionCall.callService();	
			
			Logger.verifyEquals("200", closesessionCall.getStatus(), "验证Status");
			Logger.verifyEquals("", closesessionCall.getMsg(), "验证Msg");
			Logger.verifyEquals("true", closesessionCall.getResult().get("Success").toString(), "验证Success");
			
			ymt_cssession mdoel=csSessionWapper.selectByPrimaryKey(csSession.getId());
			Logger.verifyEquals(1, mdoel.getStatus(), "验证Status=1");
			ymt_onlinecs onlinecs2=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csId)).get(0);
			Logger.verifyEquals(1, onlinecs1.getCurrentSessions()-onlinecs2.getCurrentSessions(), "验证最大会话数 减 1");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}